package android.textIt.frontend.adapter;

import java.util.List;

import android.content.Context;
import android.textIt.R;
import android.textIt.backend.model.Message;
import android.textIt.frontend.activity.MainActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MessageAdapter extends ArrayAdapter<Message> {
	 private Context context;
	 private List<Message> items;

   public MessageAdapter(Context context, List<Message> messageList) {
           super(context, R.layout.li_messages, messageList);
           this.context = context;
           this.items = messageList;
   }
   
   @Override
   public boolean areAllItemsEnabled(){
  	 return false;
   }
   
   @Override
   public boolean isEnabled(int position){ 
	   return false;
	 }

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
       if (convertView == null) {
           LayoutInflater vi = 
          	 (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           convertView = vi.inflate(R.layout.li_messages, null);
       }
       Message msg = items.get(position);
       TextView tvBubble = (TextView) convertView.findViewById(R.id.TxtBubbleMessage);
       tvBubble.setText(msg.getSender().getName() + ": " + msg.getContent());
       if (msg.getSender().getId() == MainActivity.phoneOwner.getId()){
      	 convertView.setBackgroundResource(R.drawable.bubble2);
       }
       else{
      	 convertView.setBackgroundResource(R.drawable.bubble1);      	 
       }
       //convertView.setBackgroundResource((position & 1) == 0 ? R.drawable.bubble1 : R.drawable.bubble2);
       return convertView;
   }
}
